import { Component, OnInit } from '@angular/core';
import { PageEntity } from '../../../core/domain/page.entity';
import { SemesterService } from './semester.service';
import { StatusEnum } from '../../../core/enum/status.enum';
import { NzMessageService } from 'ng-zorro-antd';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { DatePipe } from '@angular/common';
import {StorageUtil} from '../../../utils/storage.util';

@Component({
  selector: 'app-semester-manage',
  templateUrl: './semester-manage.component.html',
  providers: [SemesterService]
})
export class SemesterManageComponent implements OnInit {
  // 分页查询条件
  pageInfo: PageEntity = new PageEntity();
  data: Array<SemesterPageVO> = [];
  isSemester = false;
  validateForm: FormGroup;
  param: SemesterParam = new SemesterParam();
  selectYears: Array<SelectYears> = [];
  semesterList: Array<any> = [
    {text: '第一学期', value: 0},
    {text: '第二学期', value: 1},
    {text: '小学期', value: 2}
  ];
  dateRange = [];
  showBtns = 1; // 当前登录人是否有操作权限,0-隐藏 1-显示

  constructor(private semester: SemesterService, private datePipe: DatePipe, private fb: FormBuilder, private message: NzMessageService) { }

  ngOnInit() {
    this.page(this.pageInfo);
    this.generateYear();
    // this.showBtns = StorageUtil.getLocalStorage('roleManager');
  }

  page(page: PageEntity): void {
    this.semester.page(page).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.data = res.datas.data;
        this.pageInfo.totalRecords = res.datas.totalRecords;
      }
    });
  }

  initForm(): void {
    this.validateForm = this.fb.group({
      schoolYear: [this.param.schoolYear, [Validators.required]],
      semester: [this.param.semester, [Validators.required]],
      dateRange: [, [Validators.required]]
    });
  }

  /**
   * 排课时间
   */
  generateYear(): void {
    const starYear = 2013;
    const endYear = Number(this.datePipe.transform(new Date(), 'yyyy'));
    for (let i = endYear ; i > starYear ; i--) {
      const yearInfo = i + '-' + (i + 1);
      const pageSeach = i + ',' + (i + 1);
      this.selectYears.push({
        text: yearInfo + '学年',
        value: yearInfo,
        pageSeach
      });
    }
  }

  /**
   * 学年设置
   * @param event 学年参数
   */
  schoolYearChange(event): void {

  }

  /**
   * 学期设置
   * @param event 学期参数
   */
  semesterChange(event): void {

  }

  addbtn(): void {
    this.isSemester = true;
    this.param = new SemesterParam();
    this.dateRange = [];
    this.initForm();
  }

  editApp(item: SemesterPageVO): void {
    this.dateRange = [];
    this.param = new SemesterParam();
    this.param.schoolYear = item.schoolYear;
    this.param.semester = item.semester;
    this.param.id = item.id;
    this.dateRange.push(item.beginTime);
    this.dateRange.push(item.endTime);
    this.initForm();
    this.isSemester = true;
  }

  /**
   * @author leihfei
   * @date 2019-05-31
   * @description 提交
   */
  submit(): void {
    if (this.validateForm.valid) {
      // 取出数据，进行转换，后台保存
      this.param.beginTime = this.datePipe.transform(this.dateRange[0], 'yyyy-MM-dd');
      this.param.endTime = this.datePipe.transform(this.dateRange[1], 'yyyy-MM-dd');
      if (this.param.id) {
        this.semester.update(this.param).subscribe(res => {
          if (res.status === StatusEnum.SUCCESS) {
            this.message.success(res.message);
            this.page(this.pageInfo);
            this.isSemester = false;
          }
        });
      } else {
        this.semester.create(this.param).subscribe(res => {
          if (res.status === StatusEnum.SUCCESS) {
            this.message.success(res.message);
            this.page(this.pageInfo);
            this.isSemester = false;
          }
        });
      }
    } else {
      this.message.warning('数据校验不通过!');
    }
  }


  getSemesterName(semester: number): string {
    let result = '';
    switch (semester) {
      case 0:
        result = '第一学期';
        break;
      case 1:
        result = '第二学期';
        break;
      case 2:
        result = '小学期';
        break;
    }
    return result;
  }

  delete(ids: string): void {
    this.semester.delete(ids).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
        this.page(this.pageInfo);
      }
    });
  }
}

class SemesterPageVO {
  id?: string;
  schoolYear?: string;
  semester?: number;
  beginTime?: string;
  endTime?: string;
}

class SemesterParam {
  id?: string;
  schoolYear?: string;
  semester?: number;
  beginTime?: string;
  endTime?: string;
}

export class SelectYears {
  text?: string;
  value?: string;
  pageSeach?: string;
}
